//package com.example.demo.controller;
//
//import com.example.demo.entity.Event;
//import com.example.demo.event.EventProducer;
//import com.example.demo.util.CommunityConstant;
//import com.example.demo.util.CommunityUtil;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletResponse;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
//@Controller
//public class ShareController implements CommunityConstant {
//
//    private Logger logger = LoggerFactory.getLogger(ShareController.class);
//
//    @Autowired
//    private EventProducer eventProducer;
//
//    @Value("${community.path.domain}")
//    private String domain;
//
//    @Value("${wk.image.storage}")
//    private String storage;
//
//    @Value("${server.servlet.context-path}")
//    private String contextPath;
//
//    @Value("${qiniu.bucket.share.url}")
//    private String shareBucketUrl;
//
//    @RequestMapping(path = "/share", method = RequestMethod.GET)
//    @ResponseBody
//    public String share(String htmlUrl) {
//        // 文件名
//        String fileName = CommunityUtil.generateUUID();
//
//        // 异步生成长图
//        Event event = new Event()
//                .setTopic(TOPIC_SHARE)
//                .setData("htmlUrl", htmlUrl)
//                .setData("fileName", fileName)
//                .setData("suffix", ".png");
//        eventProducer.fireEvent(event);
//
//        // 返回访问路径
//        Map<String,Object> map = new HashMap<>();
////        map.put("shareUrl", domain + contextPath + "/share/image/" + fileName );
//        map.put("shareUrl", shareBucketUrl + "/" + fileName);
//
//        return CommunityUtil.getJSONString(0, null, map);
//
//    }
//
//    // 废弃
//    // 获取长图
//    @RequestMapping(path = "/share/image/{fileName}", method = RequestMethod.GET)
//    public void getShareImage(@PathVariable("fileName") String fileName, HttpServletResponse response) {
//        if (fileName.isEmpty()) {
//            throw new IllegalArgumentException("文件名不能为空！");
//        }
//        response.setContentType("image/png");
//        File file = new File(storage + "/" + fileName + ".png");
//
//        try {
//            ServletOutputStream outputStream = response.getOutputStream();
//            FileInputStream fileInputStream = new FileInputStream(file);
//            byte[] buffer = new byte[1024];
//            int b = 0;
//            while ((b = fileInputStream.read(buffer)) != -1) {
//                outputStream.write(buffer, 0, b);
//            }
//        } catch (IOException e) {
//            logger.info("获取长图失败！");
//        }
//
//
//    }
//}
